﻿using System;
using System.Collections.Generic;
using System.Text;

/// <summary>
/// Represents unique key for an SDMX-ML Artefact 
/// </summary>
public class ArtefactPrimaryKey
{
    #region Construction

    public ArtefactPrimaryKey(string arType, string id, string agency, string vers)
    {
        _artefactType = arType;
        _id = id;
        _agencyId = agency;
        _version = vers;
    }

    public ArtefactPrimaryKey(string arType, string fullId)
    {
        _artefactType = arType;
        if (fullId != null)
        {
            string[] arr = fullId.Split('+');
            if (arr.Length > 2 && arr.Length < 4)
            {
                _id = arr[1];
                _agencyId = arr[0];
                if (arr.Length == 3)
                    _version = arr[2];
            }
        }
    }
    #endregion

    #region Public Methods

    /// <summary>Get key as an object array</summary>
    /// <returns>the array</returns>
    public object[] GetKey()
    {
        return new object[] { Id, Version, AgencyId };
    }
    #endregion

    #region Properties

    public bool IsValid
    {
        get
        {
            //TODO Regex on ID's ?
            return string.IsNullOrEmpty(Id) == false &&
                string.IsNullOrEmpty(AgencyId) == false ;
        }
    }

    public bool IsValidType
    {
        get
        {
            return Manager.Instance.ValidType(_artefactType);
        }
    }

   public string FullIdent
    {
        get
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(AgencyId).Append('+');
            sb.Append(Id).Append('+');
            sb.Append(Version);
            return sb.ToString();
        }
    }

   private string _artefactType;
   public string ArtefactType
   {
       get { return _artefactType; }
   }
 
    private string _agencyId;
    public string AgencyId
    {
        get
        {
            return _agencyId;
        }
    }

    private string _id;
    public string Id
    {
        get
        {
            return _id;
        }
    }

    private string _version;
    public string Version
    {
        get 
        {
            if (string.IsNullOrEmpty(_version))
                _version = "1.0";

            return _version; 
        }
    }
    #endregion
}
